package com.supor.aiot.food.ui.food;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.android.baseconfig.base.BaseApiResult;
import com.android.baseconfig.common.config.HttpConstant;
import com.android.baseconfig.common.utils.ToastUtils;
import com.supor.aiot.common.app.AppAgent;
import com.supor.aiot.common.base.BaseLocalConfigBean;
import com.supor.aiot.common.base.BaseViewModel;
import com.supor.aiot.common.helper.GlobalDataCache;
import com.supor.aiot.food.api.FoodImpl;
import com.supor.aiot.food.bean.RecipeThumbsBean;
import com.supor.md_video.api.VideoApiImpl;

import org.json.JSONException;
import org.json.JSONObject;

import autodispose2.AutoDisposeConverter;
import io.reactivex.rxjava3.functions.Consumer;
import okhttp3.RequestBody;

public class FoodViewModel extends BaseViewModel {

    private MutableLiveData<BaseLocalConfigBean> recipeThumbsResult = new MutableLiveData<>();

    public FoodViewModel(@NonNull Application application) {
        super(application);
    }

    public void recipeThumb(String addrId, int position) {
        JSONObject jsonObject = new JSONObject();
        RequestBody requestBody = null;
        try {
            jsonObject.put("addrId", addrId);
            jsonObject.put("replyAddr", "RECIPE_ID");//REPLY_ID 评论
            requestBody = RequestBody.create(HttpConstant.JSON,
                    jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        FoodImpl.getInstance().recipeThumbs(requestBody)
                .subscribe(new Consumer<BaseApiResult<RecipeThumbsBean>>() {
                    @Override
                    public void accept(BaseApiResult<RecipeThumbsBean> recipeThumbsBeanBaseApiResult) throws Throwable {
                        BaseLocalConfigBean baseLocalConfigBean = new BaseLocalConfigBean();
                        baseLocalConfigBean.setLocalId(addrId);
                        baseLocalConfigBean.setLocalType(1);
                        baseLocalConfigBean.setLocalPosition(position);
                        baseLocalConfigBean.setLocalLikeId(recipeThumbsBeanBaseApiResult.data.getLikeId() == 0 ?
                                "" : String.valueOf(recipeThumbsBeanBaseApiResult.data.getLikeId()));
                        recipeThumbsResult.setValue(baseLocalConfigBean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Throwable {

                    }
                });
    }

    public void recipeThumbsDel(String addrId, String likeId, int position) {
        if (null == GlobalDataCache.userInfo || !AppAgent.isLogin()) {
            ToastUtils.getInstance().showToast(getApplication(), "请先登录");
            return;
        }
        VideoApiImpl.getInstance().commThumbsDel(likeId)
                .subscribe(new Consumer<BaseApiResult<String>>() {
                    @Override
                    public void accept(BaseApiResult<String> stringBaseApiResult) throws Exception {
                        BaseLocalConfigBean baseLocalConfigBean = new BaseLocalConfigBean();
                        baseLocalConfigBean.setLocalId(addrId);
                        baseLocalConfigBean.setLocalType(-1);
                        baseLocalConfigBean.setLocalPosition(position);
                        baseLocalConfigBean.setLocalLikeId("");
                        recipeThumbsResult.setValue(baseLocalConfigBean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
    }

    public MutableLiveData<BaseLocalConfigBean> getRecipeThumbsResult() {
        return recipeThumbsResult;
    }
}
